﻿using Dapper;
using System;
using BCEntity.TMS.VehicleSuiteBusinessConfig;
using System.Collections.Generic;
using System.Data;
using System.Text;
using System.Threading.Tasks;
using BCCommon;
using BCCommon.TMS.Transport;

namespace BCData.TMS.VehicleSuiteBusinessConfig
{
	/// <summary>
	/// 车辆套餐业务配置
	/// </summary>
	public class VehicleSuiteBusinessConfigData : IVehicleSuiteBusinessConfigData
	{
		private readonly IDatabaseContext databaseContext;
		private const string QUERY_EXISTS = @"SELECT 1 FROM vehiclesuitebusinessconfig";
		private const string QUERY = @"SELECT A.VehicleSuiteBusinessConfigId,A.IsRecommend,A.ProvinceCode,A.CityCode,A.CountyCode,A.AgentRecordId,A.CreateAdminId,A.CreateAdminName,A.CreateTime,A.UpdateTime,
											A.EditAdminId,A.VehicleTransportBusinessConfigId,A.EditAdminName,A.SuiteBusinessType,A.SuiteBusinessName,A.SuiteSlogan,A.BusinessDescriptionTitle,
											A.BusinessDescription,A.CurrentlyFee,A.SourceFee,A.Enabled,
											B.TransportBusiness,D.ProvinceName,D.CityName,D.CountyName
										FROM vehiclesuitebusinessconfig A
										INNER JOIN VehicleTransportBusinessConfig B ON A.VehicleTransportBusinessConfigId=B.VehicleTransportBusinessConfigId
										INNER JOIN County D ON A.CountyCode=D.CountyCode";

		public VehicleSuiteBusinessConfigData(IDatabaseContext databaseContext)
		{
			this.databaseContext = databaseContext;
		}
		#region 检查车辆套餐业务配置是否存在
		public async Task<bool> Exists(int vehicleSuiteBusinessConfigId)
		{
			string sql = string.Format(@"{0} WHERE VehicleSuiteBusinessConfigId=?VehicleSuiteBusinessConfigId LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessConfigId", vehicleSuiteBusinessConfigId, DbType.Int32);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		public async Task<bool> Exists(long vehicleTransportBusinessConfigId, VehicleSuiteBusinessType suiteBusinessType, string countyCode, long agentRecordId)
		{
			string sql = string.Format(@"{0} WHERE VehicleTransportBusinessConfigId=?VehicleTransportBusinessConfigId 
												AND SuiteBusinessType=?SuiteBusinessType 
												AND AgentRecordId=?AgentRecordId
												AND CountyCode=?CountyCode LIMIT 1", QUERY_EXISTS);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleTransportBusinessConfigId", vehicleTransportBusinessConfigId, DbType.Int32);
			parameters.Add("?SuiteBusinessType", suiteBusinessType.ToString(), DbType.AnsiString);
			parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64);
			parameters.Add("?CountyCode", countyCode, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteScalarAsync(sql, parameters) != null;
		}
		#endregion

		#region 添加车辆套餐业务配置
		public async Task<VehicleSuiteBusinessConfigEntity> Add(VehicleSuiteBusinessConfigEntity entity)
		{
			string sql = SqlCommands.GetInsertSql("vehiclesuitebusinessconfig", new string[] { "IsRecommend", "ProvinceCode", "CityCode", "CountyCode", "AgentRecordId", 
				"CreateAdminId", "CreateAdminName", "VehicleTransportBusinessConfigId", "SuiteBusinessType", "SuiteBusinessName", "SuiteSlogan", "BusinessDescriptionTitle",
				"BusinessDescription", "CurrentlyFee", "SourceFee" });
			sql = string.Format("{0} {1} WHERE A.VehicleSuiteBusinessConfigId=LAST_INSERT_ID();", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?IsRecommend", entity.IsRecommend, DbType.Boolean);
			parameters.Add("?ProvinceCode", entity.ProvinceCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CityCode", entity.CityCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CountyCode", entity.CountyCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?AgentRecordId", entity.AgentRecordId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminId", entity.CreateAdminId, DbType.Int64, size: 20);
			parameters.Add("?CreateAdminName", entity.CreateAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?VehicleTransportBusinessConfigId", entity.VehicleTransportBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?SuiteBusinessType", entity.SuiteBusinessType.ToString(), DbType.AnsiString, size: 50);
			parameters.Add("?SuiteBusinessName", entity.SuiteBusinessName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?SuiteSlogan", entity.SuiteSlogan?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?BusinessDescriptionTitle", entity.BusinessDescriptionTitle?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?BusinessDescription", entity.BusinessDescription?.Trim(), DbType.String);
			parameters.Add("?CurrentlyFee", entity.CurrentlyFee, DbType.Decimal, size: 10);
			parameters.Add("?SourceFee", entity.SourceFee, DbType.Decimal, size: 10);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessConfigEntity>(sql, parameters, databaseContext.Transaction);

		}
		#endregion

		#region 修改车辆套餐业务配置
		public async Task<VehicleSuiteBusinessConfigEntity> Update(VehicleSuiteBusinessConfigEntity entity)
		{
			string sql = SqlCommands.GetUpdateSql("vehiclesuitebusinessconfig", new string[] { "IsRecommend", "ProvinceCode", "CityCode", "CountyCode",
					"EditAdminId", "VehicleTransportBusinessConfigId", "EditAdminName", "SuiteBusinessType", "SuiteBusinessName", "SuiteSlogan", "BusinessDescriptionTitle", 
					"BusinessDescription", "CurrentlyFee", "SourceFee" }, new string[] { "VehicleSuiteBusinessConfigId" });
			sql = string.Format("{0} {1} WHERE A.VehicleSuiteBusinessConfigId=?VehicleSuiteBusinessConfigId;", sql, QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessConfigId", entity.VehicleSuiteBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?IsRecommend", entity.IsRecommend, DbType.Boolean);
			parameters.Add("?ProvinceCode", entity.ProvinceCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CityCode", entity.CityCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?CountyCode", entity.CountyCode?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?EditAdminId", entity.EditAdminId, DbType.Int64, size: 20);
			parameters.Add("?VehicleTransportBusinessConfigId", entity.VehicleTransportBusinessConfigId, DbType.Int32, size: 11);
			parameters.Add("?EditAdminName", entity.EditAdminName?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?SuiteBusinessType", entity.SuiteBusinessType.ToString(), DbType.AnsiString, size: 50);
			parameters.Add("?SuiteBusinessName", entity.SuiteBusinessName?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?SuiteSlogan", entity.SuiteSlogan?.Trim(), DbType.AnsiString, size: 50);
			parameters.Add("?BusinessDescriptionTitle", entity.BusinessDescriptionTitle?.Trim(), DbType.AnsiString, size: 20);
			parameters.Add("?BusinessDescription", entity.BusinessDescription?.Trim(), DbType.String);
			parameters.Add("?CurrentlyFee", entity.CurrentlyFee, DbType.Decimal, size: 10);
			parameters.Add("?SourceFee", entity.SourceFee, DbType.Decimal, size: 10);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessConfigEntity>(sql, parameters, databaseContext.Transaction);
		}
		#endregion

		#region 获取车辆套餐业务配置
		public async Task<VehicleSuiteBusinessConfigEntity> GetEntity(int vehicleSuiteBusinessConfigId)
		{
			string sql = string.Format(@"{0} WHERE A.VehicleSuiteBusinessConfigId=?VehicleSuiteBusinessConfigId LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessConfigId", vehicleSuiteBusinessConfigId, DbType.Int64);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessConfigEntity>(sql, parameters);
		}
		public async Task<VehicleSuiteBusinessConfigEntity> GetEntity(long vehicleTransportBusinessConfigId, VehicleSuiteBusinessType suiteBusinessType, string countyCode, long agentRecordId)
		{
			string sql = string.Format(@"{0} WHERE A.VehicleTransportBusinessConfigId=?VehicleTransportBusinessConfigId 
												AND A.SuiteBusinessType=?SuiteBusinessType 
												AND A.AgentRecordId=?AgentRecordId
												AND A.CountyCode=?CountyCode LIMIT 1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleTransportBusinessConfigId", vehicleTransportBusinessConfigId, DbType.Int32);
			parameters.Add("?SuiteBusinessType", suiteBusinessType.ToString(), DbType.AnsiString);
			parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64);
			parameters.Add("?CountyCode", countyCode, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.QueryFirstOrDefaultAsync<VehicleSuiteBusinessConfigEntity>(sql, parameters);
		}
		#endregion

		#region 获取车辆套餐业务配置列表
		public async Task<IEnumerable<VehicleSuiteBusinessConfigEntity>> GetList(long agentRecordId)
		{
			string sql = string.Format(@"{0} WHERE A.AgentRecordId=?AgentRecordId AND A.Enabled=1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64, size: 20);
			return await this.databaseContext.Master.QueryAsync<VehicleSuiteBusinessConfigEntity>(sql, parameters);
		}

		public async Task<IEnumerable<VehicleSuiteBusinessConfigEntity>> GetList(long agentRecordId, string countyCode, int vehicleTransportBusinessConfigId)
		{
			string sql = string.Format(@"{0} WHERE A.AgentRecordId=?AgentRecordId AND A.CountyCode=?CountyCode AND A.VehicleTransportBusinessConfigId=?VehicleTransportBusinessConfigId AND A.Enabled=1", QUERY);
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?AgentRecordId", agentRecordId, DbType.Int64, size: 20);
			parameters.Add("?CountyCode", countyCode, DbType.AnsiString, size: 20);
			parameters.Add("?VehicleTransportBusinessConfigId", vehicleTransportBusinessConfigId, DbType.Int32);
			return await this.databaseContext.Master.QueryAsync<VehicleSuiteBusinessConfigEntity>(sql,parameters);
		}
		#endregion

		#region 获取车辆套餐业务配置分页
		public async Task<(IEnumerable<VehicleSuiteBusinessConfigEntity>, int)> Query(long? agentRecordId, string suiteBusinessName, string provinceCode, string cityCode, string countyCode, VehicleSuiteBusinessType? suiteBusinessType,bool? enabled, DateTime? start, DateTime? end, string sortName, bool? ascending, int pageIndex, int pageSize)
		{
			List<string> wheres = new List<string>();
			DynamicParameters parameters = new DynamicParameters();
            if (agentRecordId.HasValue)
            {
				wheres.Add("A.AgentRecordId=?AgentRecordId");
				parameters.Add("?AgentRecordId", agentRecordId.Value, DbType.Int64);
			}

			if (!string.IsNullOrWhiteSpace(suiteBusinessName))
			{
				wheres.Add("A.SuiteBusinessName LIKE CONCAT('%',CONCAT(?SuiteBusinessName,'%'))");
				parameters.Add("?SuiteBusinessName", suiteBusinessName, DbType.AnsiString);
			}
			if (!string.IsNullOrWhiteSpace(provinceCode))
			{
				wheres.Add("A.ProvinceCode=?ProvinceCode");
				parameters.Add("?ProvinceCode", provinceCode, DbType.AnsiString);
			}
			if (!string.IsNullOrWhiteSpace(cityCode))
			{
				wheres.Add("A.CityCode=?CityCode");
				parameters.Add("?CityCode", cityCode, DbType.AnsiString);
			}
			if (!string.IsNullOrWhiteSpace(countyCode))
			{
				wheres.Add("A.CountyCode=?CountyCode");
				parameters.Add("?CountyCode", countyCode, DbType.AnsiString);
			}
			if (suiteBusinessType.HasValue)
            {
				wheres.Add("A.SuiteBusinessType=?SuiteBusinessType");
				parameters.Add("?SuiteBusinessType", suiteBusinessType.Value.ToString(), DbType.AnsiString);
			}
			if (enabled.HasValue)
			{
				wheres.Add("A.Enabled=?Enabled");
				parameters.Add("?Enabled", enabled.Value, DbType.Boolean);
			}
			if (start.HasValue && !end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start");
				parameters.Add("?Start", start.Value, DbType.DateTime);
			}
			else if (!start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime<=?End");
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			else if (start.HasValue && end.HasValue)
			{
				wheres.Add("A.CreateTime>=?Start AND A.CreateTime<=?End");
				parameters.Add("?Start", start.Value, DbType.DateTime);
				parameters.Add("?End", end.Value, DbType.DateTime);
			}
			string sql = string.Format(@"{0} {1} ORDER BY {4} {5} LIMIT {2},{3}; SELECT COUNT(1) FROM vehiclesuitebusinessconfig A
										INNER JOIN VehicleTransportBusinessConfig B ON A.VehicleTransportBusinessConfigId=B.VehicleTransportBusinessConfigId
										INNER JOIN County D ON A.CountyCode=D.CountyCode {1}",
				  QUERY,
				  wheres.Count > 0 ? " WHERE " + string.Join(" AND ", wheres) : "",
				  (pageIndex == 0 ? 0 : pageIndex - 1) * pageSize,
				  pageSize,
				  string.IsNullOrWhiteSpace(sortName) ? "A.CreateTime" : "A." + sortName,
				  ascending.HasValue && ascending.Value ? "ASC" : "DESC");
			return await this.databaseContext.QueryMultipleAsync<VehicleSuiteBusinessConfigEntity, int>(sql, parameters);
		}

		#endregion

		#region 启用车辆套餐业务配置
		public async Task<bool> Enabled(int vehicleSuiteBusinessConfigId, long adminId, string fullname)
		{
			string sql = @"UPDATE vehiclesuitebusinessconfig SET Enabled=1,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE VehicleSuiteBusinessConfigId=?VehicleSuiteBusinessConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessConfigId", vehicleSuiteBusinessConfigId, DbType.Int64);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}
		#endregion

		#region 禁用车辆套餐业务配置
		public async Task<bool> Disabled(int vehicleSuiteBusinessConfigId, long adminId, string fullname)
		{
			string sql = @"UPDATE vehiclesuitebusinessconfig SET Enabled=0,EditAdminId=?EditAdminId,EditAdminName=?EditAdminName WHERE VehicleSuiteBusinessConfigId=?VehicleSuiteBusinessConfigId";
			DynamicParameters parameters = new DynamicParameters();
			parameters.Add("?VehicleSuiteBusinessConfigId", vehicleSuiteBusinessConfigId, DbType.Int64);
			parameters.Add("?EditAdminId", adminId, DbType.Int64, size: 20);
			parameters.Add("?EditAdminName", fullname, DbType.AnsiString, size: 20);
			return await this.databaseContext.Master.ExecuteAsync(sql, parameters, databaseContext.Transaction) > 0;
		}

		#endregion
	}
}



